package springboot.springboot06_ssmp.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springboot.common_utils.R;
import springboot.springboot06_ssmp.entity.User;
import springboot.springboot06_ssmp.mapper.UserMapper;
import springboot.springboot06_ssmp.service.IUserService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author daiqiang
 * @since 2024-06-24
 */
@Slf4j
@CrossOrigin  //允许跨域
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private IUserService userService;
    @Resource
    private UserMapper userMapper;

    @ApiOperation("用户登录2024-06 ")  //接口中文说明
    @PostMapping("/login")   //增  20231212
    public R<Map<String, Object>> login(@RequestBody User User) {
        // md5摘要加密
        User.setPassWord(SecureUtil.md5(User.getPassWord()));
        Map<String,Object> userdata= userService.login2(User);
        if (userdata != null) {
            return R.success(userdata);
        }
        return R.fail(20001, "用户名或密码错误");

    }
    @GetMapping("/getInfo")   //查  20231212
    public R<Map<String, Object>> info(@RequestParam("token")String token) {
        Map<String, Object> userdata = userService.getUserInfo(token);


        if (userdata != null) {
            return R.success(userdata);
        }
        return R.fail(20003, "登录信息无效，请重新登录");
    }
    @PostMapping("/logout")   //增  退出登录
    public R <?> logout(@RequestHeader("token")String token) {   //需与前端携带阐述一致
        //根据token 获取用户信息
        userService.logout(token);
        return R.success();
    }

    @GetMapping
    public R getAll(){
        return R.success(userService.list());
    }


    //分页查询
    @GetMapping("{currentPage}/{pageSize}")
    public R getAllPage(@PathVariable int currentPage,@PathVariable int pageSize,User user){
        System.out.println("参数----》"+user);
        IPage<User> pageUser=userService.getPage(currentPage,pageSize,user );

        return R.success(pageUser);

    }

    //分页条件查询
    @GetMapping("/list")
    public R<Map<String,Object>> getUserList(@RequestParam(value="userName",required=false) String  userName,
                                             @RequestParam (value="phone",required=false) String  phone,
                                             @RequestParam (value="pageNo",required=false) Long  pageNo,
                                             @RequestParam (value="pageSize",required=false) Long  pageSize,
                                             @RequestParam(value="sortField", required = false, defaultValue = "id") String sortField,
                                             @RequestParam(value="sortOrder", required = false, defaultValue = "desc") String sortOrder // 新增排序字段和排序顺序
    ){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.hasLength(userName),User::getUserName,userName);
        lqw.eq(StringUtils.hasLength(phone),User::getPhone,phone);
        // 根据sortField和sortOrder进行排序
        if (StringUtils.hasLength(sortField)) {
            boolean isDesc = "desc".equalsIgnoreCase(sortOrder);
            if (isDesc) {
                lqw.orderByDesc(User::getId);
            } else {
                lqw.orderByAsc(User::getId);
            }
        }
        Page<User> page = new Page<>(pageNo,pageSize);
        userMapper.selectPage(page,lqw);
        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());
        return R.success(data);

    }
    @PostMapping
    public R save(@RequestBody User user){

        userService.addUser(user);

        return R.success("success");
    }
    @PutMapping
    public  R update(@RequestBody User user){
        userService.updateUserById(user);
        return  R.success("修改成功！");
    }
    @DeleteMapping("{id}")
    public  R delete(@PathVariable Integer id){
        userService.deleteUserById(id);
        return  R.success("成功删除用户！");
    }
    @GetMapping("{id}")
    public R getById(@PathVariable Integer id) {

        return  R.success(userService.getUserById(id));
    }

}
